﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsFilesTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Files Business Object
    /// </summary>

    public class Files : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Files() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// FileType.
        /// </summary>
        public string FileType
        {
            get { return _fileType; }
            set
            {
                if (_fileType != value)
                {
                    if (OnPropertyChanging("FileType", _fileType, value))
                    {
                        string oldValue = _fileType;
                        _fileType = value;
                        OnPropertyChanged("FileType", oldValue, value);
                    }
                }
            }
        }  private string _fileType = null;


        /// <summary>
        /// FileSize.
        /// </summary>
        public string FileSize
        {
            get { return _fileSize; }
            set
            {
                if (_fileSize != value)
                {
                    if (OnPropertyChanging("FileSize", _fileSize, value))
                    {
                        string oldValue = _fileSize;
                        _fileSize = value;
                        OnPropertyChanged("FileSize", oldValue, value);
                    }
                }
            }
        }  private string _fileSize = null;


        /// <summary>
        /// FileInfo.
        /// </summary>
        public string FileInfo
        {
            get { return _fileInfo; }
            set
            {
                if (_fileInfo != value)
                {
                    if (OnPropertyChanging("FileInfo", _fileInfo, value))
                    {
                        string oldValue = _fileInfo;
                        _fileInfo = value;
                        OnPropertyChanged("FileInfo", oldValue, value);
                    }
                }
            }
        }  private string _fileInfo = null;


        /// <summary>
        /// FileTitle.
        /// </summary>
        public string FileTitle
        {
            get { return _fileTitle; }
            set
            {
                if (_fileTitle != value)
                {
                    if (OnPropertyChanging("FileTitle", _fileTitle, value))
                    {
                        string oldValue = _fileTitle;
                        _fileTitle = value;
                        OnPropertyChanged("FileTitle", oldValue, value);
                    }
                }
            }
        }  private string _fileTitle = null;


        /// <summary>
        /// FileDescription.
        /// </summary>
        public string FileDescription
        {
            get { return _fileDescription; }
            set
            {
                if (_fileDescription != value)
                {
                    if (OnPropertyChanging("FileDescription", _fileDescription, value))
                    {
                        string oldValue = _fileDescription;
                        _fileDescription = value;
                        OnPropertyChanged("FileDescription", oldValue, value);
                    }
                }
            }
        }  private string _fileDescription = null;


        /// <summary>
        /// Date/time at which the Files was created.
        /// </summary>
        public DateTime? UploadedDate
        {
            get { return _uploadedDate; }
            set
            {
                if (_uploadedDate != value)
                {
                    if (OnPropertyChanging("UploadedDate", _uploadedDate, value))
                    {
                        DateTime? oldValue = _uploadedDate;
                        _uploadedDate = value;
                        OnPropertyChanged("UploadedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _uploadedDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Files object.
        /// </summary>
        public static Files CreateNew()
        {
            Files obj = new Files();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.UploadedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Files CreateNew(FilesCollection collection)
        {
            Files obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetCompleteFiles

        /// <summary>
        /// Returns a collection of Files.
        /// </summary>
        public static FilesCollection GetCompleteFiles(int userid)
        {
            FilesTableAdapter adapter = new FilesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsFiles.FilesDataTable tbl = adapter.GetData(userid);

            FilesCollection list = new FilesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsFiles.FilesRow row in tbl.Rows)
                {
                    Files files = new Files();
                    RelationalToObject(files, row);
                    list.Add(files);
                }
            }

            return (list);
        }

        #endregion

        #region Delete

        /// <summary>
        /// Delete a file.
        /// </summary>
        public static int Delete(int userid, string fileinfo)
        {
            FilesTableAdapter adapter = new FilesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            int d = adapter.Delete(userid, fileinfo);

            return (d);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Files.
        /// </summary>
        public static void Update(Files obj)
        {
            bool updateFiles = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateFiles = (obj.ObjState != ObjectState.Unchanged);

                if (!updateFiles)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsFiles.FilesDataTable tbl = null;
            if (updateFiles)
            {
                FilesTableAdapter adapter = new FilesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsFiles.FilesDataTable();
                dsFiles.FilesRow row = tbl.NewFilesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("UserID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Files: UserID={0}, FileInfo={1}, {2}", UserID, FileInfo));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class FilesCollection : List<Files>
    {
        #region Types

        public class UserComparer : IComparer<Files>
        {
            public int Compare(Files f1, Files f2)
            {
                return (String.Compare(f1.FileInfo, f2.FileInfo));
            }
        }

        #endregion

        #region Public Methods

        #region FindByUserID

        public Files FindByUserID(int userid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].UserID == userid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
